//
// CrackUpdaterEntriesReader
//
// Copyright (C) jextra.net.
//
//  This file is part of the Crack build system.
//
//  The Crack build system is free software; you can redistribute it 
//  and/or modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Crack build system is distributed in the hope that it will be 
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Crack build system; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.crack.updater.http;

import java.util.*;
import net.jextra.crack.core.*;
import net.jextra.crack.updater.*;
import org.xml.sax.*;

/**
 * Reads XML from web-service to get timestamps of files.
 */
public class CrackUpdaterEntriesReader extends XMLSAXReader
{
    // ============================================================
    // Enums
    // ============================================================

    public enum State
    {
        entries( "crack-updater-entries" ), 
        entry( "entry" );

        private String tag;

        State( String tag )
        {
            this.tag = tag;
        }

        public String tag()
        {
            return tag;
        }
    }

    public enum Attribute
    {
        path( "path" ), 

        type( "type" ), 
        timestamp( "timestamp" ), 
        size( "size" ), 
        sha256Hash( "sha-256-hash" );

        String _tag;

        Attribute( String tag )
        {
            _tag = tag;
        }

        public String tag()
        {
            return _tag;
        }
    }

    // ============================================================
    // Fields
    // ============================================================

    private String _path;
    private ArrayList<CrackUpdaterEntry> _entries;

    // ============================================================
    // Constructors
    // ============================================================

    public CrackUpdaterEntriesReader()
    {
        _entries = new ArrayList<CrackUpdaterEntry>();
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public String getPath()
    {
        return _path;
    }

    public void setPath( String path )
    {
        _path = path;
    }

    public List<CrackUpdaterEntry> getEntries()
    {
        return _entries;
    }

    // ----------
    // protected
    // ----------

    @Override
    protected void startDocument()
    {
    }

    @Override
    protected void endDocument()
    {
    }

    @Override
    protected String startElement( String stateString, String uri, String tagName, String qName,
        Attributes atts )
    {
        State newState = null;

        if ( tagName.equalsIgnoreCase( State.entries.tag() ) )
        {
            newState = State.entries;

            _path = atts.getValue( Attribute.path.tag() );
        }
        else if ( tagName.equalsIgnoreCase( State.entry.tag() ) )
        {
            newState = State.entry;

            String typeString = atts.getValue( Attribute.type.tag() );
            CrackUpdaterEntry.Type type = CrackUpdaterEntry.Type.getTagType( typeString );
            String path = atts.getValue( Attribute.path.tag() );
            long timestamp = 0L;
            long size = 0L;
            String sha256Hash = null;
            if ( type == CrackUpdaterEntry.Type.file )
            {
                timestamp = Long.parseLong( atts.getValue( Attribute.timestamp.tag() ) );
                size = Long.parseLong( atts.getValue( Attribute.size.tag() ) );
                sha256Hash = atts.getValue( Attribute.sha256Hash.tag() );
            }
            _entries.add( new CrackUpdaterEntry( path, type, timestamp, size, sha256Hash ) );
        }

        return newState == null ? null : newState.name();
    }

    @Override
    protected void characters( String stateString, char[] value, int offset, int count )
    {
    }

    @Override
    protected void endElement( String stateString, String uri, String tagName, String qName )
    {
    }
}

